package com.zys.el.user.quartz;

import com.zys.el.user.po.TimeInfo;
import com.zys.el.user.service.ITimeInfoService;
import com.zys.el.user.util.html.acm;
import com.zys.el.user.util.html.comput;
import com.zys.el.user.util.html.lanQiao;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

public class InfoQuartz extends QuartzJobBean {

    @Autowired
    private comput comput;

    @Autowired
    private lanQiao lanQiao;

    @Autowired
    private acm  acm;

    @Autowired
    private ITimeInfoService timeInfoService;


    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            System.out.println("计算机设计大赛官网数据加载...");
            Map<String, Date> computNew = comput.getNew();
//            System.out.println(computNew);
            for (String s : computNew.keySet()) {
                boolean b = timeInfoService.IsHaveInfo(s, computNew.get(s)); // 存在为true 不存在为false
//                System.out.println(b);
                // 不存在就是最新，添加
                if (b == false) {
                    TimeInfo timeInfo = new TimeInfo();
                    timeInfo.setPublicTime(computNew.get(s));
                    timeInfo.setItem(s.replaceAll("\n", ""));
                    timeInfo.setTypeId(1L);
                    timeInfoService.saveOrUpdate(timeInfo);
                }
            }

            System.out.println("蓝桥杯官网数据加载...");
            Map<String, Date> lanQiaoNew = lanQiao.getNew();
            for (String s : lanQiaoNew.keySet()) {
                boolean b = timeInfoService.IsHaveInfo(s, lanQiaoNew.get(s)); // 存在为true 不存在为false
                // 不存在就是最新，添加
                if (b == false) {
                    TimeInfo timeInfo = new TimeInfo();
                    timeInfo.setPublicTime(lanQiaoNew.get(s));
                    timeInfo.setItem(s.replaceAll("\n", ""));
                    timeInfo.setTypeId(2L);
                    timeInfoService.saveOrUpdate(timeInfo);
                }
            }

            System.out.println("ACM官网数据加载...");
            Map<String, Date>  acmNew = acm.getNew();
            for (String s : acmNew.keySet()) {
                boolean b = timeInfoService.IsHaveInfo(s, acmNew.get(s)); // 存在为true 不存在为false
                // 不存在就是最新，添加
                if (b == false) {
                    TimeInfo timeInfo = new TimeInfo();
                    timeInfo.setPublicTime(acmNew.get(s));
                    timeInfo.setItem(s.replaceAll("\n", ""));
                    timeInfo.setTypeId(3L);
                    timeInfoService.saveOrUpdate(timeInfo);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

//    private void Computer() throws IOException, ParseException {

//    }
//
//    private void lanQiao() throws IOException, ParseException {

//    }
}